
#include	"Enemy.h"
#include	"defs.h"

Enemy::Enemy(){
	
}

bool Enemy::init(char* shipImage, eShipTypes st){

	if(!Object::init(shipImage))
		return false;

	eShipType = st;

	weapon.setEnemyWeapon(true);

	switch(eShipType)
	{
		case ESHIP_1:

			if(!weapon.init(Weapon::PULSE_GUN, this))
				return false;

			collisionRect.x = 11;
			collisionRect.y = 4;
			collisionRect.w = 38;
			collisionRect.h = 39;

			shipHealth = 10;

			weapon.setFireRate(Weapon::PULSE_GUN_RATE + 200);

			shipSpeed = ESHIP_1_SPEED;
			shipState = SHIP_FORWARD;

			speedY_multiplier = 0.3f;

			weapon.isFiring = true;

		break;

		default:
			return false;
	}
	
	return true;
}

void Enemy::loop(){

	moveEnemy();

	if( shipState & SHIP_FORWARD )
		speedY = shipSpeed * speedY_multiplier;
	else if( shipState & SHIP_BACKWARD )
			speedY = -shipSpeed * speedY_multiplier;
		 else
			speedY = 0;

	if( shipState & SHIP_LEFT )
		speedX = -shipSpeed * speedX_multiplier;
	else if( shipState & SHIP_RIGHT )
			speedX = shipSpeed * speedX_multiplier;
		 else
			speedX = 0;

	//In case of enemy y increases normally
	speedY *= -1;

	Object::loop();

	if(x < 0)
	{
		x = 0;
		unsetLeft();
	}

	if(x > WND_WIDTH - objectSurface->w / 4)
	{
		x = WND_WIDTH - objectSurface->w / 4;
		unsetRight();
	}

	weapon.loopProjectiles();
}

void Enemy::render(SDL_Surface* destSurface){
	
	if(shipHealth <= 0)
		return;

	Surface::OnDraw(destSurface, objectSurface, x, y);

	weapon.renderProjectiles(destSurface);
}

void Enemy::moveEnemy(){
	
	switch(eShipType)
	{
		case ESHIP_1:

			if(shipState == SHIP_FORWARD)
			{
				/*if( rand() % 10 + 1 <= 5 )
					setRight();
				else
					setLeft();*/
			}
			else
			{
				if(speedX == 0)
				{
					if(x <= WND_WIDTH / 2)
						setRight();
					else
						setLeft();
				}
			}


		break;
	}
}
